//
//  MTChatRoomTopRateView.swift
//  MT
//
//  Created by ooops on 2020/5/14.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import RollingNotice_Swift

class MTChatRoomTopRateView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
        noticeView.reloadDataAndStartRoll()
    }
    
    private func setupUI() {
        addSubview(noticeView)
        addSubview(rightArrowImageView)
    }
    
    private func makeConstraint() {
        noticeView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        rightArrowImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-8)
            make.width.height.equalTo(15)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    lazy var rightArrowImageView: UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "arrow_right"))
        return imageV
    }()
    
    private var result: [MTRankingInfoModel] = []
    
    lazy var noticeView: GYRollingNoticeView = {
        let v = GYRollingNoticeView()
        v.stayInterval = 5
        v.register(MTChatRoomTopRateCell.self, forCellReuseIdentifier: MTChatRoomTopRateCell.reuseIdentity())
        v.delegate = self
        v.dataSource = self
        return v
    }()
    
    var model: MTRankingModel? {
        didSet {
            if let result = model?.result, result.count > 0 {
                self.result = result
                self.noticeView.reloadDataAndStartRoll()
            }
        }
    }
    
//    fileprivate func creatRoolingViewWith(arr: Array<Any>, isFirst: Bool) {
//        let w = UIScreen.main.bounds.size.width
//        var frame = CGRect.init(x: 0, y: 150, width: w, height: 50)
//        if !isFirst {
//            frame = CGRect.init(x: 0, y: 250, width: w, height: 30)
//        }
//
//        noticeView.reloadDataAndStartRoll()
//    }

}

extension MTChatRoomTopRateView: GYRollingNoticeViewDelegate, GYRollingNoticeViewDataSource {
    
    func numberOfRowsFor(roolingView: GYRollingNoticeView) -> Int {
        return self.result.count
    }
    
    func rollingNoticeView(roolingView: GYRollingNoticeView, cellAtIndex index: Int) -> GYNoticeViewCell {
        let cell: MTChatRoomTopRateCell = roolingView.dequeueReusableCell(withIdentifier: MTChatRoomTopRateCell.reuseIdentity()) as! MTChatRoomTopRateCell
        let model = self.result[index]
        cell.model = model
        return cell

    }
    
    
    func rollingNoticeView(_ roolingView: GYRollingNoticeView, didClickAt index: Int) {
        debugPrint("did click index: \(roolingView.currentIndex)")
        let model = self.result[roolingView.currentIndex]
        if let nav = ((UIApplication.shared.keyWindow?.rootViewController as? BaseTabBarViewController)?.selectedViewController as? BaseNavController) {
            nav.pushViewController(MTRateNestViewController(type: model.rankingType == 1 ? MTDiscoverTableFooterViewType.charm : MTDiscoverTableFooterViewType.strength), animated: true)
        }
        
    }
    
}
